let host = 'cn-beijing.log.aliyuncs.com';
let project = 'zhufengmonitor';
let logStore = 'zhufengmonitor-store';
let userAgent = require('user-agent');
function getExtraData() {
    return {
        title: document.title,
        url: location.href,
        timestamp: Date.now(),
        userAgent: userAgent.parse(navigator.userAgent)
    }
}

class SendTracker{
    constructor(){
        this.url = `http://${project}.${host}/logstores/${logStore}/track`;//上报的路径
        this.xhr = new XMLHttpRequest;
    }
    send(data = {}){
        let extraData = getExtraData();
        let log = {...extraData, ...data}
        for(let key in log){
            if(typeof log[key] === 'number'){
                log[key] = `${log[key]}`
            }
        }
        let body = JSON.stringify(log);
        console.log('上报数据', log)
        this.xhr.open('POST', this.url, true);
        this.xhr.setRequestHeader('Content-Type', 'application/json');
        this.xhr.setRequestHeader('x-log-apiversion', '0.6.0');
        this.xhr.setRequestHeader('x-log-bodyrawsize', body.length);
        this.xhr.onload = function () {
            // console.log(this.xhr.response);
        }
        this.xhr.onerror = function (error) {
            console.log(error);
        }
        this.xhr.send(body);
    }
}

export default new SendTracker();